package com.javafactroy.kunlong.codefactroy.service.tool;


import cn.hutool.core.util.StrUtil;
import com.javafactroy.kunlong.base.config.datasource.DatasourceConfig;
import com.javafactroy.kunlong.base.config.datasource.mysql.MysqlDatasourceConfig;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataTable;
import com.javafactroy.kunlong.codefactroy.consts.IdbType;
import com.javafactroy.kunlong.codefactroy.convert.MetadataColumnPropConvert;
import com.javafactroy.kunlong.codefactroy.convert.MetadataTablePropConvert;
import com.javafactroy.kunlong.codefactroy.service.domain.MetadataPropAgg;
import com.javafactroy.kunlong.data.dto.MetadataColumnPropApiDto;
import com.javafactroy.kunlong.data.dto.MetadataTablePropApiDto;
import com.javafactroy.kunlong.data.entity.mysql.MysqlTables;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProps;
import com.javafactroy.kunlong.data.dao.mysql.MysqlTablesMapper;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public final class DbCodeMysqlTool extends DbCodetool{


    @Autowired
    MysqlDatasourceConfig mysqlDatasourceConfig;

    @Autowired
    MysqlTablesMapper mysqlTablesMapper;

    void make(DatasourceConfig datasourceConfig) {
        if (datasourceConfig.getDbName().equals(IdbType.DbType_Mysql)) {
            makeMysqls();
        }
    }

    public void makeMysqls() {
        //information_schema.TABLES

        loadMysqlTables().forEach(i -> makeMysql(i.getTableName()));
    }

    void saveMetadataProp(MetadataTable metadataTable) {

        MetadataPropAgg agg = new MetadataPropAgg();
        MetadataTablePropApiDto dto = MetadataTablePropConvert.INSTANCE.toOne(metadataTable);
        dto.setClsName(StrUtil.toCamelCase(dto.getTableName()));
        List<MetadataColumnPropApiDto> list = MetadataColumnPropConvert.INSTANCE.toList(metadataTable.getFieldList());
        agg.setMetadataTablePropApiDto(dto);
        agg.setColList(list);

        metadataPropService.saveTableProp(agg);

    }

    public void makeMysql(String table) {

        MetadataTable metadata = metadataModel.loadDbMysqlMetadata(table);
        saveMetadataProp(metadata);
        makeFile(metadata);
    }

    public void makeMysqlLike(String tableLike) {

        loadMysqlTables().stream().filter(i -> i.getTableName().contains(tableLike)).forEach(i -> makeMysql(i.getTableName()));
    }

    void makeFile(MetadataTable metadata) {
        EntityProps entityProps = metadata.toEntityProps();
        entityProps.makeParams();
        log.info("template params={}", CommonUtils.toJSONString(entityProps.getStringTemplate().getParams()));

        if (rmFile) {
            fileTemplateFactroy.rmDbFile(metadata);
            rmFile = false;
        }
        fileTemplateFactroy.getList().forEach(i -> {

            String stext = i.valueOf(entityProps).parse(i.readFile());
            i.writeFile(stext);
        });

    }


    List<MysqlTables> loadMysqlTables() {
        String dbName = mysqlDatasourceConfig.getDbName();
        List<MysqlTables> tables = mysqlTablesMapper.select(new MysqlTables().setDbName(dbName));
        log.info("tables = {}", CommonUtils.toJSONString(tables));
        return tables;
    }

    List<MysqlTables> loadMysqlTables(String tableName) {
        String dbName = mysqlDatasourceConfig.getDbName();
        List<MysqlTables> tables = mysqlTablesMapper.select(new MysqlTables().setDbName(dbName).setTableName(tableName));
        log.info("tables = {}", CommonUtils.toJSONString(tables));
        return tables;
    }

}
